package com.webserver.controller;

import com.webserver.annotation.Controller;
import com.webserver.annotation.RequestMapping;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;
import com.webserver.util.DBUtil;

import java.io.File;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 用于处理用户相关业务
 *
 * @author Chen
 */
@Controller
public class UserController {

    private static File userDir;

    static {
        userDir = new File("./users");
        if (!userDir.exists()) {
            userDir.mkdirs();
        }
    }

    @RequestMapping("/regUser")
    public void reg(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始处理用户注册！");

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        String ageStr = request.getParameter("age");
        System.out.println(username + "," + password + "," + nickname + "," + ageStr);
        if (username == null || username.isEmpty()
                || password == null || password.isEmpty()
                || nickname == null || nickname.isEmpty()
                || ageStr == null || ageStr.isEmpty()
                || !ageStr.matches("[0-9]+")
        ) {
            System.out.println("用户信息输入错误，注册失败！");
            response.sendRedirect("/reg_info_error.html");
            return;
        }

        System.out.println(username + "," + password + "," + nickname + "," + ageStr);
        int age = Integer.parseInt(ageStr);

        try (
                Connection connection = DBUtil.getConnection()
        ) {
            Statement statement = connection.createStatement();
            String sql = "INSERT INTO userinfo " +
                    "(username,password,nickname,age) " +
                    "VALUES " +
//                       "('','','',12)";
                    "('" + username + "','" + password + "','" + nickname + "'," + age + ")";
            System.out.println(sql);
            int count = statement.executeUpdate(sql);
            if (count > 0) {
                response.sendRedirect("/reg_success.html");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @RequestMapping("/loginUser")
    public void login(HttpServletRequest request, HttpServletResponse response) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        if (username == null || username.isEmpty() || password == null || password.isEmpty()) {
            System.out.println("用户输入信息错误!");
            response.sendRedirect("/login_info_error.html");
            return;
        }

        try (
                Connection connection = DBUtil.getConnection()
        ) {
            Statement statement = connection.createStatement();
            String sql = "SELECT id,username,password,nickname,age " +
                    "FROM userinfo " +
                    "WHERE username='" + username + "' " +
                    "AND password='" + password + "' ";
            ResultSet resultSet = statement.executeQuery(sql);
            if (resultSet.next()) {
                response.sendRedirect("/login_success.html");
            } else {
                response.sendRedirect("/login_fail.html");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
